package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.ShareContent;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMusic;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;
import com.tiantianyuegou.activity.BaseActivity;
import com.tiantianyuegou.R;
import com.tiantianyuegou.app.SysApplication;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.Tools;
import com.tiantianyuegou.view.MyDialogTextView;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 支付成功的分享界面
 * Created by ivany on 2016/1/15.
 */
public class PaySuccessActivity extends BaseActivity implements View.OnClickListener {


    private TextView mTxt_tip;
    private ImageButton mBtn_weixin, mBtn_friend, mBtn_qq, mBtn_weibo;
    private String mOrderId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_success);
        SysApplication.getInstance().shareInit();//分享的设置初始化及相关初始化
       Tools.changeSystemBar(this);
        setTitle("支付完成");
        initViews();
        //将activity加入栈
        SysApplication.getInstance().addActivity(this);
    }

    @Override
    public void findViewByIds() {
        mTxt_tip = (TextView) findViewById(R.id.pay_success_tip);
      /*  mBtn_weixin = (ImageButton) findViewById(R.id.pay_success_share_weixin);
        mBtn_friend = (ImageButton) findViewById(R.id.pay_success_share_friend);
        mBtn_qq = (ImageButton) findViewById(R.id.pay_success_share_qq);
        mBtn_weibo = (ImageButton) findViewById(R.id.pay_success_share_weibo);*/
    }

    @Override
    public void initListener() {
      /*  mBtn_weixin.setOnClickListener(this);
        mBtn_friend.setOnClickListener(this);
        mBtn_qq.setOnClickListener(this);
        mBtn_weibo.setOnClickListener(this);*/
    }

    @Override
    public void initData() {
        String value = getIntent().getStringExtra("value");

        mOrderId = getIntent().getStringExtra("orderId");

        mTxt_tip.setText("恭喜您,已经成功付款" + value.substring(1,value.length()) + "元完成购买");
    }

    @Override
    public void onClick(View v) {
        //设置分享的图片
        UMImage image = new UMImage(PaySuccessActivity.this, R.mipmap.ic_launcher);
      /*  switch (v.getId()) {
            case R.id.pay_success_share_weixin:
                new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN).setCallback(umShareListener)
                        .withText("我正在使用一个超好用的电商软件，推荐你也用起来哦")
                        .withMedia(image)
                        .withTargetUrl("http://www.baidu.com")
                        .share();
                break;
            case R.id.pay_success_share_friend:
                new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(umShareListener)
                        .withText("我正在使用一个超好用的电商软件，推荐你也用起来哦")
                        .withMedia(image)
                        .withTargetUrl("http://www.baidu.com")
                        .share();
                break;
            case R.id.pay_success_share_qq:
                new ShareAction(this).setPlatform(SHARE_MEDIA.QQ).setCallback(umShareListener)
                        .withText("我正在使用一个超好用的电商软件，推荐你也用起来哦")
                        .withMedia(image)
                        .withTargetUrl("http://www.baidu.com")
                        .share();
                break;
            case R.id.pay_success_share_weibo:
                new ShareAction(this).setPlatform(SHARE_MEDIA.SINA).setCallback(umShareListener)
                        .withText("我正在使用一个超好用的电商软件，推荐你也用起来哦")
                        .withMedia(image)
                        .withTargetUrl("http://www.baidu.com")
                        .share();
                break;
        }*/
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
//            Toast.makeText(PaySuccessActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
            String paltformName = platform.toString();
            if (paltformName.contains("QQ")) {
                addShareRecord("2");
            } else if (paltformName.contains("WEIXIN")) {
                addShareRecord("1");
            } else if (paltformName.contains("SINA")) {
                addShareRecord("3");
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(PaySuccessActivity.this, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(PaySuccessActivity.this, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** attention to this below ,must add this**/
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    //分享成功后调用，获取红博鳌个数接口
    private void addShareRecord(String platform) {
        final LoadingDialog loadingDialog = new LoadingDialog(this);
        loadingDialog.setMsg("正在获取红包...");
        loadingDialog.show();
        RequestParams params = new RequestParams();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        params.addBodyParameter("tsPlatform", platform);
        params.addBodyParameter("tpId", mOrderId);
        params.addBodyParameter("type", "2");
        Log.e("TAG","add share Record:"+SPHelper.getTmId()+":"+platform+":"+mOrderId);
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.ADD_SHARE, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                loadingDialog.dismiss();
                Log.e("TAG", "add share record:" + responseInfo.result);
                if (TextUtils.isEmpty(responseInfo.result)) {
                    showMsg("获取红包失败");
                } else {
                    try {
                        JSONObject root = new JSONObject(responseInfo.result);
                        String code=root.getString("code");
                        if("100".equals(code)){
                            int red=root.getInt("red");
                            MyDialogTextView dialog=new MyDialogTextView(PaySuccessActivity.this,"分享成功","恭喜您获得了"
                                    +red+"个红包");
                            dialog.btn_cancel.setVisibility(View.GONE);
                            dialog.setDialogCallback(new MyDialogTextView.DialogCallback() {
                                @Override
                                public void dialogOpt() {
                                    Intent intent = new Intent(PaySuccessActivity.this, MyOrderActivity.class);
                                    intent.putExtra("type", "share");
                                    startActivity(intent);
                                }
                            });
                            dialog.show();
                        }else{
                            String msg=root.getString("message");
                            showMsg(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        showMsg("解析异常");
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                loadingDialog.dismiss();
                Log.e("TAG", "add shareRecord failure:" + s);
                showMsg("当前网络不可用,请检查您的网络设置");
            }
        });
    }

}
